<?php
/**
 * 支付管理
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');

class OrderController extends AdminbaseController{
	//支付设置
	public function order_set(){
		$data = M("pay_type")->select();
		$this->assign("data",$data);
		$this->display();
	}

//账户 二维码
   public function zhifubao(){
	   $data =  M("pay_zhifubao") ->order("dengji asc")->select();
	   $this->assign("data",$data);
	   $this->display();
   }

//支付宝账户添加
   public function zhifubao_add(){
	   $dengji = I("dengji");
	   $img = I("img");
	   $date = M("pay_zhifubao")->where("dengji={$dengji}")->select();
	   if($date){
		   $this->error("该等级已添加过二维码图片，请修改等级");
		   die;
	   }
	   $config = array(
			   "maxSize" => '3145728',// 设置附件上传大小
			   "exts" => 'jpg,gif,png,jpeg',// 设置附件上传类型
	   );

	   $upload = new \Think\Upload($config);// 实例化上传类
	   // 上传文件
	   $info   =   $upload->upload($img);

	   $http = sp_get_host();
	   $path = '';
	   foreach($info as $v){ //拼接路径
		   $path .= "{$http}/Uploads/".trim($v['savepath'],'.').$v['savename'];
	   }

	   $data['dengji'] = $dengji;
	   $data['img'] = $path;
	   $data['name'] = I("name");
	   $data['zhanghao'] = I("zhanghao");
	   $data['number'] = I("yinhangka");
	   $data['zhihang'] = I("zhihang");
	   $data['yinhang'] = I("yinhang");
	   $data['ctime'] = time();
	   M("pay_zhifubao")->add($data);
	   $this->success("添加成功",U("Order/zhifubao"));
   }

//支付宝账户修改
	public function zhifubao_upd()
	{
		if(IS_POST){
			$dengji = I("dengji");
			$data = M("pay_zhifubao")->where("dengji=$dengji")->find();
			$img = I("img1");
			$config = array(
					"maxSize" => '3145728',// 设置附件上传大小
					"exts" => 'jpg,gif,png,jpeg',// 设置附件上传类型

			);
			$upload = new \Think\Upload($config);// 实例化上传类

			// 上传文件
			$info   =   $upload -> upload($img);

			$http = sp_get_host();
			$path = '';
			foreach($info as $v){ //拼接路径
				$path .= "{$http}/Uploads/".trim($v['savepath'],'.').$v['savename'];
			}
			if($path){
				$data1['img'] = $path;
			}else{
				$data1['img'] = $data['img'];
			}
			$data1['name'] = I("name");
			$data1['zhanghao'] = I("zhanghao");
			$data1['number'] = I("yinhangka");
			$data1['zhihang'] = I("zhihang");
			$data1['yinhang'] = I("yinhang");
			$data1['ctime'] = time();
			M("pay_zhifubao")->where("dengji = {$dengji}")->save($data1);
			$this->success("修改成功",U("Order/zhifubao"));
		}else{
			$dengji= I("dengji");
			$data = M("pay_zhifubao")->where("dengji=$dengji")->find();
			$this->assign("da",$data);
			$this->assign("dengji",$dengji);
			$this->display();
		}
	}
//支付宝账户删除
   public function zhifubao_del(){
	   $id = I("id");
	   $r = M("pay_zhifubao")->where("id=$id")->delete();
	   if($r){
		   $this->success("删除成功",U("Order/zhifubao"));
	   }else{
		   $this->error("删除失败");
	   }
   }

//账户 二维码
	public function weixin(){
		$data =  M("pay_weixin") ->order("dengji asc")->select();
		$this->assign("data",$data);
		$this->display();
	}

//微信账户添加
	public function weixin_add(){
		$dengji = I("dengji");
		$img = I("img");
		$date = M("pay_weixin")->where("dengji={$dengji}")->select();
		if($date){
			$this->error("该等级已添加过二维码图片，请修改等级");
			die;
		}
		$config = array(
				"maxSize" => '3145728',// 设置附件上传大小
				"exts" => 'jpg,gif,png,jpeg',// 设置附件上传类型
		);

		$upload = new \Think\Upload($config);// 实例化上传类
		// 上传文件
		$info   =   $upload->upload($img);

//		if(!$info) {// 上传错误提示错误信息
//			$this->error($upload->getError());
//		}
		$http = sp_get_host();
		$path = '';
		foreach($info as $v){ //拼接路径
			$path .= "{$http}/Uploads/".trim($v['savepath'],'.').$v['savename'];
		}

		$data['dengji'] = $dengji;
		$data['img'] = $path;
		$data['name'] = I("name");
		$data['number'] = I("yinhangka");
		$data['zhihang'] = I("zhihang");
		$data['yinhang'] = I("yinhang");
		$data['zhanghao'] = I("zhanghao");
		$data['ctime'] = time();
		M("pay_weixin")->add($data);
		$this->success("添加成功",U("Order/weixin"));
	}

//微信账户修改
	public function weixin_upd()
	{
		if(IS_POST){
			$dengji = I("dengji");
			$data = M("pay_weixin")->where("dengji=$dengji")->find();
			$img = I("img1");

			$config = array(
					"maxSize" => '3145728',// 设置附件上传大小
					"exts" => 'jpg,gif,png,jpeg',// 设置附件上传类型

			);
			$upload = new \Think\Upload($config);// 实例化上传类

			// 上传文件
			$info   =   $upload -> upload($img);

//			if(!$info) {// 上传错误提示错误信息
//				$this->error($upload->getError());
//			}
			$http = sp_get_host();
			$path = '';
			foreach($info as $v){ //拼接路径
				$path .= "{$http}/Uploads/".trim($v['savepath'],'.').$v['savename'];
			}
			if($path){
				$data1['img'] = $path;
			}else{
				$data1['img'] = $data['img'];
			}
			$data1['ctime'] = time();
			$data1['name'] = I("name");
			$data1['number'] = I("yinhangka");
			$data1['zhihang'] = I("zhihang");
			$data1['yinhang'] = I("yinhang");
			$data1['zhanghao'] = I("zhanghao");
			M("pay_weixin")->where("dengji = {$dengji}")->save($data1);
			$this->success("修改成功",U("Order/weixin"));
		}else{
			$dengji= I("dengji");
			$data = M("pay_weixin")->where("dengji=$dengji")->find();
			$this->assign("da",$data);
			$this->assign("dengji",$dengji);
			$this->display();
		}
	}
//微信账户删除
	public function weixin_del(){
		$id = I("id");
		$r = M("pay_weixin")->where("id=$id")->delete();
		if($r){
			$this->success("删除成功",U("Order/weixin"));
		}else{
			$this->error("删除失败");
		}
	}
//银行卡
	public function bankcard(){
		$data =  M("pay_bankcard") ->order("dengji asc")->select();
		$this->assign("data",$data);
		$this->display();
	}

//银行卡账户添加
	public function bankcard_add(){
		if($_POST){
		$dengji = I("dengji");
		$date = M("pay_bankcard")->where("dengji={$dengji}")->select();
		if($date){
			$this->error("该等级已添加过银行卡，请修改等级");
			die;
		}else{
			$data2['dengji'] = $dengji;
		}
		$data2['name'] = I("name");
		$data2['number'] = I("yinhangka");
		$data2['zhihang'] = I("zhihang");
		$data2['yinhang'] = I("yinhang");
		$data2['ctime'] = time();
		M("pay_bankcard")->add($data2);
		$this->success("添加成功",U("Order/bankcard"));
		}else{
			$this->display();
		}
	}
//银行卡账户修改
	public function  bankcard_upd()
	{
		if(IS_POST){
			$dengji = I("dengji");
			$data2['dengji'] = $dengji;
			$data2['name'] = I("name");
			$data2['number'] = I("yinhangka");
			$data2['zhihang'] = I("zhihang");
			$data2['yinhang'] = I("yinhang");
			$data2['ctime'] = time();
			M("pay_bankcard")->where("dengji = {$dengji}")->save($data2);
			$this->success("修改成功",U("Order/bankcard"));
		}else{
			$dengji= I("dengji");
			$data = M("pay_bankcard")->where("dengji=$dengji")->find();
			$this->assign("da",$data);
			$this->assign("dengji",$dengji);
			$this->display();
		}
	}
//银行卡账户删除
	public function bankcard_del(){
		$id = I("id");
		$r = M("pay_bankcard")->where("id=$id")->delete();
		if($r){
			$this->success("删除成功",U("Order/bankcard"));
		}else{
			$this->error("删除失败");
		}
	}


	//充值记录
	public function pay_record(){
		$accounts = trim(I("accounts"));
		$grade = I("grade");//支付方式
		$dengji= I("dengji");
		$kaishi = I("kaishi");
		$jieshu = I("jieshu");


		$p = I("p");

		$where = array();
		//账号搜索
		if(!empty($accounts)){
			$where['t.my_name'] = array('LIKE',"$accounts");
		}
		//支付方式搜索
		if(!empty($grade)){
			$where['t.type'] = array('EQ',"$grade");
		}
        //等级搜索
		if(!empty($dengji)){
			if($dengji==1){
				$where['a.score'] = array(array('EGT','0'),array('LT','10'));
			}elseif($dengji==2){
				$where['a.score'] = array(array('EGT','10'),array('LT','200'));
			}elseif($dengji==3){
				$where['a.score'] = array(array('EGT','200'),array('LT','1000'));
			}elseif($dengji==4){
				$where['a.score'] = array(array('EGT','1000'),array('LT','10000'));
			}elseif($dengji==5){
				$where['a.score'] = array(array('EGT','10000'),array('LT','50000'));
			}elseif($dengji==6){
				$where['a.score'] = array(array('EGT','50000'),array('LT','250000'));
			}elseif($dengji==7){
				$where['a.score'] = array(array('EGT','250000'),array('LT','1000000'));
			}elseif($dengji==8){
				$where['a.score'] = array(array('EGT','1000000'),array('LT','5000000'));
			}elseif($dengji==9){
				$where['a.score'] = array('EGT','5000000');
			}
		}
 //时间搜索
		if(!empty($kaishi)){
			$start = strtotime($kaishi);
			$where['t.ctime'] = array('EGT',$start);
		}
		if(!empty($jieshu)){
			$end = strtotime($jieshu);
			$where['t.ctime'] = array('ELT',$end);
		}
		
		$count= M("order t")
			   	->join("join ".C("DB_PREFIX")."accountsinfo a on t.uid = a.userid")
				->where($where)
				->count();
		if($count==0){
			$p = 0;
		}else{
			if(empty($p)){
				$p = 1;
			}
		}
		$totalpage = ceil($count/10);
		$page = $this->page($count,10);
		$res= M("order t")
			   	->join("join ".C("DB_PREFIX")."accountsinfo a on t.uid = a.userid")
				->where($where)
				->limit($page->firstRow, $page->listRows)
				->field("t.*,a.nickname,a.score,a.accounts")
				->order("ctime desc")
				->select();
		$this->assign("accounts",$accounts);
		$this->assign("grade",$grade);
		$this->assign("dengji",$dengji);
		$this->assign("res",$res);
		$this->assign("kaishi",$kaishi);
		$this->assign("jieshu",$jieshu);
		$this -> assign("p",$p);
		$this -> assign("totalpage",$totalpage);
		$this -> assign("page",$page->show('Admin'));
		$this->display();
	}


//充值列表
	public function chongzhi(){
          $id = I("id");//order表的id
          $dd['state'] = 1;
		  M('order')->where("id = '{$id}'")->save($dd);
		  $order = M('order')->where("id = '{$id}'")->find();

		  $Accountsinfo = M('Accountsinfo')->where("userid = ".$order['uid'])->field("userid,money")->find();

       //往用户表里添加 money
		M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("money",$order['money']);
		M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("score",$order['money']);
		M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("sheng_money",$order['money']);

		//往交易表里添加 数据
		$cp['number'] = $order['number'];
		$cp['type'] = '1';
		$cp['shou_money'] = $order['money'];
		$cp['ky_money'] = $Accountsinfo['money'] + $order['money'];
		$cp['ctime'] = time();
		$cp['uid'] = $order['uid'];
		$cp['remark'] = '充值入款';
		M("jiaoyi")->add($cp);

		//往活动表里 变化
		$co = M('Accountsinfo')->where("userid = ".$order['uid'])->field("score")->find();
		if($co['score']>=0 && $co['score']<10){
			$vip = 1;
		}if($co['score']>=10 && $co['score']<200){
			$vip = 2;
		}if($co['score']>=200 && $co['score']<1000){
			$vip = 3;
		}if($co['score']>=1000 && $co['score']<10000){
			$vip = 4;
		}if($co['score']>=10000 && $co['score']<50000){
			$vip = 5;
		}if($co['score']>=50000 && $co['score']<250000){
			$vip = 6;
		}if($co['score']>=250000 && $co['score']<1000000){
			$vip = 7;
		}if($co['score']>=1000000 && $co['score']<5000000){
			$vip = 8;
		}if($co['score']>=5000000){
			$vip = 9;
		}
		$ro = M('up_level')->where("uid = ".$order['uid'])->field("level")->find();
		if($vip >= 3 && $order['money']>=100){
			M('up_level')->where("uid = '{$order['uid']}' and type = 5")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
		}
		if($vip!=$ro['level']){
			M('up_level')->where("uid = ".$order['uid'])->save(array("level"=>$vip));
			M('up_level')->where("uid = '{$order['uid']}' and type = 1")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
		}

//去掉活动 推广好礼 里边的值 begian 618

        $acc =  M('Accountsinfo')->where("userid = ".$Accountsinfo['userid'])->field("yq_code")->find();

		$agent = M("agent")->where("code=".$acc['yq_code'])->field("uid")->select();//查邀请码


		foreach($agent as $v){
			$uid = $v['uid'];
		}

		$ag = M("agent")->where("uid={$uid}")->field("code")->select();//查邀请码

		foreach($ag as $v){
			$code[] = $v["code"];
		}
		$codes = implode(",",$code);

		//查找所有code下注册的账号
		if(!empty($codes)){
			$Accountsinfo1 = M('Accountsinfo')->where("yq_code in($codes)")->field("score")->select();
		}
		$zong = 0;
		foreach($Accountsinfo1 as $v){
			$zong += $v['score'];
		}
		$lingqu = M('up_level')->where("uid = '{$uid}' and type = 7")->find();
		if($lingqu['kl_money']==58.00){}elseif($zong>=5000){
			M('up_level')->where("uid = '{$uid}' and type = 7")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
		}
//去掉活动 推广好礼 里边的值 end


		$this->success("完成充值",U("Order/pay_record"));
	}
	//取消充值
	public function qx_chongzhi(){
		$id = I("id");//order表的id
		$dd['state'] = 2;
		M('order')->where("id = '{$id}'")->save($dd);
		$this->success("取消充值成功",U("Order/pay_record"));
	}



	//提现记录
	public function tx_record(){
		$accounts = I("accounts");
		$start_time = I("start_time");
		$end_time = I("end_time");
		$p = I("p");
		$where = array();
		if(!empty($accounts)){
			$where['a.nickname'] = array('LIKE',"%$accounts%");
		}
		if(!empty($start_time)){
			$start = strtotime($start_time." 00:00:00");
			$where['t.ctime'] = array('EGT',$start);
		}
		if(!empty($end_time)){
			$end = strtotime($end_time." 23:59:59");
			$where['t.ctime'] = array('ELT',$end);
		}
		$re = M("region")->select();
		$gi = arr_val_key($re,"id");
		$count= M("tixian t")
			   	->join("join ".C("DB_PREFIX")."accountsinfo a on t.uid = a.userid")
				->join("join ".C("DB_PREFIX")."bankcard b on t.bank_id = b.id")
				->where($where)
				->count();
		$size = $page = $this->page($count, 10);
		$res= M("tixian t")
			   	->join("join ".C("DB_PREFIX")."accountsinfo a on t.uid = a.userid")
				->join("join ".C("DB_PREFIX")."bankcard b on t.bank_id = b.id")
				->where($where)
				->limit($page->firstRow, $page->listRows)
				->field("t.*,a.nickname,b.bankid,b.province,b.city,b.uname,b.card,a.accounts")
				->order("ctime desc")
				->select();

		$this->assign("res",$res);

		$this->assign("gi",$gi);
		$this->assign("p",$p);
		$this->assign("count",$count);
		$this->assign("page", $page->show('Admin'));
		$this->display();
	}
	//提现
	public function tix(){
		$id = I("id");
		$p = I("p");
		M("tixian")->where("id = $id")->save(array("is_ti"=>1,"ti_time"=>time()));
		$this->success("保存成功",U("Order/tx_record",array("p"=>$p)));
	}
	//驳回
	public function juj(){
		$id = I("id");
		$p = I("p");
		M("tixian")->where("id = $id")->save(array("is_ti"=>2,"ti_time"=>time()));
		$r = M("tixian")->where("id = $id")->find();
		$mm = M("accountsinfo")->where("userid = '{$r['uid']}'")->find();
		$cp['number'] = date("Ymd").rand(100000,999999);
		$cp['type'] = '6';
		$cp['shou_money'] = $r['money'];
		$cp['ky_money'] = $mm['money'] + $r['money'];
		$cp['ctime'] = time();
		$cp['uid'] = $r['uid'];
		$cp['remark'] = '提现退回';
		M("jiaoyi")->add($cp);
		M("accountsinfo")->where("userid = '{$r['uid']}'")->setInc("money",$r['money']);
		$this->success("保存成功",U("Order/tx_record",array("p"=>$p)));
	}
	/*
	 第三方支付设置
	*/
	function three(){
	       $data =  M("pay_three")->select();
	       $this->assign("data",$data);
	       $this->display();  
	}
	/*
	  修改第三方平台状态
	*/
	function status_set(){
	     $id= I("id");
		 $status = I("status");
		 if($status==1){
		      M("pay_three")->where("1=1")->save(array('status'=>0));
			  $data =  M("pay_three")->where("id={$id}")->save(array('status'=>1));
			  if($data){
			       $this->success("保存成功",U("Order/three"));
			  }else{
			       $this->error("系统错误",U("Order/three"));
			  } 
		 }elseif($status==0){
		      $data =  M("pay_three")->where("id={$id}")->save(array('status'=>0));
			  if($data){
			       $this->success("保存成功",U("Order/three"));
			  }else{
			       $this->error("系统错误",U("Order/three"));
			  }
		 }else{
		      $this->error("系统错误");
		 }
	}
	/*
	 *  财务入款
	 * */
	function caiwu(){
		$p = I("p");
		$accounts = I("accounts");

		$where = '1=1';
		if(!empty($accounts)){
			$where = "accounts like '%{$accounts}%'";
		}

		$count= M("Accountsinfo")
			->where($where)
			->count();

		if($count==0){
			$p = 0;
		}else{
			if(empty($p)){
				$p = 1;
			}
		}
		$totalpage = ceil($count/10);
		$page = $this->page($count,10);
		$data =M('Accountsinfo')->where($where)->field("userid,accounts,money")->limit($page->firstRow, $page->listRows)->select();
		$this->assign("arr",$data);
		$this->assign("accounts",$accounts);
		$this->assign("p",$p);
		$this->assign("totalpage",$totalpage);
		$this->assign("page", $page->show('Admin'));
        $this->display();
    }

	//添加
	function tianjia_yue(){
		$userid = I("userid");
		$tianjia = I("tianjia");
		$data =M('Accountsinfo')->where("userid = {$userid}")->field("money")->find();
		$date['money'] = $data['money']+$tianjia;
		M('Accountsinfo')->where("userid = {$userid}")->save($date);
		echo 1;
	}

	//扣除金额
	function kouchu_yue(){
		$userid = I("userid");
		$kouchu = I("kouchu");
		$data =M('Accountsinfo')->where("userid = {$userid}")->field("money")->find();
		$date['money'] = $data['money']-$kouchu;
		M('Accountsinfo')->where("userid = {$userid}")->save($date);
		echo 1;
	}



}
?>